//
//  GRUploadManager.m
//  YC_GRice
//
//  Created by LiuFeng on 2018/7/21.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRUploadManager.h"
#import "UIImage+common.h"
#import <QiniuSDK.h>

@interface GRUploadManager ()

@property (nonatomic, strong) QNUploadManager *uploadManager;

@end

@implementation GRUploadManager

+ (instancetype)defaultManager {
    static GRUploadManager *upload = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        upload = [[GRUploadManager alloc] init];
    });
    
    return upload;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _uploadManager = [[QNUploadManager alloc] init];
    }
    return self;
}

- (void)uploadWithPictures:(NSArray<UIImage *> *)pictures completion:(void(^)(NSArray *imgURLs, NSDictionary *option))completion {
    
}


- (void)uploadPicturesInBatchOrder:(NSDictionary *)option completion:(void(^)(NSDictionary *successOption, NSDictionary *failOption))completion {
    
}




- (void)uploadWithVideo:(NSString *)filepath  KeyStr:(NSString *)keyStr  UploadToken:(NSString *)uploadToken completion:(void(^)(NSString *videoURL, BOOL success))completion {
    QNUploadOption *progress = [[QNUploadOption alloc] initWithProgressHandler:^(NSString *key, float percent) {
        NSLog(@"%.2f",percent);
    }];
    
    WEAKSELF
    dispatch_queue_t queue_global = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue_global, ^{
        [weakSelf.uploadManager putFile:filepath key:keyStr token:uploadToken complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
            if (info.isOK) {
                NSString *URL = [NSString stringWithFormat:@"%@%@",QNUPLOADADDRESS,resp[@"key"]];
                completion(URL, YES);
            }else {
                completion(@"", NO);
            }
        } option:progress];
    });
}
- (void)uploadWithVideo:(NSString *)filepath completion:(void(^)(NSString *videoURL, BOOL success))completion {
    QNUploadOption *progress = [[QNUploadOption alloc] initWithProgressHandler:^(NSString *key, float percent) {
        NSLog(@"%.2f",percent);
    }];

WEAKSELF
    dispatch_queue_t queue_global = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue_global, ^{
        [weakSelf.uploadManager putFile:filepath key:nil token:[GRUserBasisInfo UploadToken] complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
            if (info.isOK) {
                NSString *URL = [NSString stringWithFormat:@"%@%@",QNUPLOADADDRESS,resp[@"key"]];
                completion(URL, YES);
            }else {
                completion(@"", NO);
            }
        } option:progress];
    });
}


- (void)uploadSingleIMAGE:(UIImage *)image completion:(void(^)(NSString *imgURL, BOOL success))completion {
    QNUploadOption *progress = [[QNUploadOption alloc] initWithProgressHandler:^(NSString *key, float percent) {
        NSLog(@"%.2f",percent);
    }];
    
    NSData *data = [image imageCompression:UIImageCompressionHighly];
    [self.uploadManager putData:data key:nil token:[GRUserBasisInfo UploadToken] complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        if (info.isOK) {
            NSString *URL = [NSString stringWithFormat:@"%@%@",QNUPLOADADDRESS,resp[@"key"]];
            completion(URL, YES);
        }else {
            completion(@"", NO);
        }
    } option:progress];
}

@end
